<template>
  <div class="light-control" :style="backgroundStyle">
    <div class="control-panel">
      <h2>MM补光灯</h2>
      
      <van-button 
        :type="isOn ? 'primary' : 'default'"
        block
        @click="toggleLight"
        class="power-button"
      >
        {{ isOn ? '关闭' : '开启' }}
      </van-button>

      <div class="slider-section">
        <div class="slider-label">
          <span>亮度</span>
          <span class="value">{{ brightness }}%</span>
        </div>
        <van-slider 
          v-model="brightness"
          :disabled="!isOn"
          @change="updateBrightness"
        />
      </div>

      <div class="slider-section">
        <div class="slider-label">
          <span>色温</span>
          <span class="value">{{ temperature }}K</span>
        </div>
        <van-slider 
          v-model="temperature"
          :min="2700"
          :max="6500"
          :step="100"
          :disabled="!isOn"
          @change="updateTemperature"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

const isOn = ref(false)
const brightness = ref(50)
const temperature = ref(5000)

// 计算背景样式
const backgroundStyle = computed(() => {
  if (!isOn.value) {
    return {
      backgroundColor: '#242424'
    }
  }
  
  // 将色温转换为RGB颜色
  const kelvinToRGB = (kelvin: number) => {
    const temp = kelvin / 100
    let r, g, b
    
    if (temp <= 66) {
      r = 255
      g = temp
      g = 99.4708025861 * Math.log(g) - 161.1195681661
      if (temp <= 19) {
        b = 0
      } else {
        b = temp - 10
        b = 138.5177312231 * Math.log(b) - 305.0447927307
      }
    } else {
      r = temp - 60
      r = 329.698727446 * Math.pow(r, -0.1332047592)
      g = temp - 60
      g = 288.1221695283 * Math.pow(g, -0.0755148492)
      b = 255
    }
    
    return {
      r: Math.min(255, Math.max(0, r)),
      g: Math.min(255, Math.max(0, g)),
      b: Math.min(255, Math.max(0, b))
    }
  }

  const rgb = kelvinToRGB(temperature.value)
  const brightnessPercent = brightness.value / 100

  return {
    backgroundColor: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${brightnessPercent})`
  }
})

const toggleLight = () => {
  isOn.value = !isOn.value
}

const updateBrightness = (value: number) => {
  brightness.value = value
}

const updateTemperature = (value: number) => {
  temperature.value = value
}
</script>

<style scoped>
.light-control {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background-color 0.3s ease;
}

.control-panel {
  background: rgba(0, 0, 0, 0.8);
  padding: 20px;
  border-radius: 12px;
  width: 80%;
  max-width: 400px;
}

h2 {
  color: white;
  margin-bottom: 20px;
  text-align: center;
}

.slider-section {
  margin: 20px 0;
}

.slider-label {
  display: flex;
  justify-content: space-between;
  color: white;
  margin-bottom: 10px;
}

.power-button {
  margin: 20px 0;
}

:deep(.van-slider) {
  margin: 10px 0;
}
</style>
